<template>
    <div>
        <div>
            <div style="display: flex;justify-content: space-between">
                <div>
                    <el-input placeholder="请输入人员名称进行搜索，可以直接回车搜索..." prefix-icon="el-icon-search"
                              clearable
                              @clear="initCarManager"
                              style="width: 350px;margin-right: 10px" v-model="keyword"
                              @keydown.enter.native="initCarManager" :disabled="showAdvanceSearchView"></el-input>
                    <el-button icon="el-icon-search" type="primary" @click="initCarManager"
                               :disabled="showAdvanceSearchView">
                        搜索
                    </el-button>
                </div>
            </div>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="CarManagers"
                    stripe
                    border
                    v-loading="loading"
                    element-loading-text="正在加载..."
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        align="left"
                        fixed
                        label="人员编号"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="mname"
                        label="姓名"
                        width="85">
                </el-table-column>
                <el-table-column
                        prop="age"
                        width="90"
                        label="年龄">
                </el-table-column>
                <el-table-column
                        prop="sex"
                        width="120"
                        label="性别">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        width="160"
                        label="入职时间">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        width="200"
                        label="最新分配时间">
                </el-table-column>
                <el-table-column
                        prop="updateBy"
                        width="200"
                        fixed="right"
                        label="操作人">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        width="200"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button @click="mallocCar(scope.row)" type="primary" style="padding: 3px"
                                   size="mini">分配车辆</el-button>
<!--                        <el-button @click="deleteManager(scope.row)" style="padding: 3px" size="mini"-->
<!--                                   type="danger">删除 </el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @current-change="currentChange"
                        @size-change="sizeChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>
        </div>
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="100%">
            <el-table
                    :data="carMs"
                    stripe
                    border
                    v-loading="loading"
                    element-loading-text="正在加载..."
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="width: 100%">
                <el-table-column
                        prop="id"
                        align="left"
                        fixed
                        label="车辆ID"
                        width="170">
                </el-table-column>
                <el-table-column
                        prop="model"
                        label="品牌型号"
                        fixed
                        align="left"
                        width="85">
                </el-table-column>
                <el-table-column
                        prop="insstatu"
                        width="120"
                        align="left"
                        label="保险情况">
                </el-table-column>
                <el-table-column
                        prop="yearstatu"
                        width="70"
                        label="年检情况">
                </el-table-column>
                <el-table-column
                        prop="mileage"
                        width="110"
                        label="总里程">
                </el-table-column>
                <el-table-column
                        prop="spend"
                        width="140"
                        label="总花销￥">
                </el-table-column>
                <el-table-column
                        prop="borrow"
                        width="100"
                        label="借用情况">
                    <template slot-scope="scope">
                        <span v-if="scope.row.borrow===0">未借出</span>
                        <span v-if="scope.row.borrow===1" style="color: #b2e281">已借出</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        width="160"
                        label="录入时间">
                </el-table-column>
                <el-table-column
                        prop="createBy"
                        width="180"
                        align="left"
                        label="录入人">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        width="160"
                        label="最近更新时间">
                </el-table-column>
                <el-table-column
                        prop="updateBy"
                        width="160"
                        label="更新人">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        width="240"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button  style="padding: 3px" size="mini" type="primary" @click="mallocToM(scope.row)">
                            分配该车辆
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @current-change="currentChange"
                        @size-change="sizeChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "CarManagerInfo",
        data() {
            return {
                searchValue: {
                    carbrand: null,
                    carmaster: null,
                    name: null
                },
                title: '',
                borrowCar:{
                    mileage:0.00,
                    spend:0.00
                },
                importDataBtnIcon: 'el-icon-upload2',
                importDataDisabled: false,
                showAdvanceSearchView: false,
                CarManagers: [],
                carMs:[],
                mallocTo:{},
                loading: false,
                popVisible: false,
                popVisible2: false,
                dialogVisible: false,
                total: 0,
                page: 1,
                keyword: '',
                size: 10,
                nations: [],
                joblevels: [],
                politicsstatus: [],
                positions: [],
                tiptopDegrees: ['本科', '大专', '硕士', '博士', '高中', '初中', '小学', '其他'],
                options: [{
                    value: 1,
                    label: '同意'
                }, {
                    value: '拒绝',
                    label: '双皮奶'
                }, {
                    value: '选项3',
                    label: '蚵仔煎'
                }, {
                    value: '选项4',
                    label: '龙须面'
                }, {
                    value: '选项5',
                    label: '北京烤鸭'
                }],
                inputDepName: '所属部门',
                mallocToMs: {
                    id: "",
                    cId: "",
                    mId: "",
                },
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                rules: {
                    brand: [{required: true, message: '请输入车辆品牌', trigger: 'blur'}],
                    model: [{required: true, message: '请输入车辆型号', trigger: 'blur'}],
                    telphone: [{required: true, message: '请输入电话号码', trigger: 'blur'}],
                    address: [{required: true, message: '请输入车主联系地址', trigger: 'blur'}],
                    carmaster: [{required: true, message: '请输入车主姓名', trigger: 'blur'}],
                    price: [{required: true, message: '请输入车辆价值', trigger: 'blur'}],
                    enginenum: [{required: true, message: '请输入车架号', trigger: 'blur'}],
                }
            }
        },
        mounted() {
            this.initCarManager();
            // this.initData();
            // this.initPositions();
        },
        methods: {
            mallocToM(data){
                 this.mallocToMs.cId=data.id;
                 this.mallocToMs.mId=this.mallocTo
                this.postRequest("/manager/basic/CarToM",this.mallocToMs).then({
                    if (resp) {
                        this.dialogVisible = false;
                        this.reload();
                    }
                })
            },
            searvhViewHandleNodeClick(data) {
                this.inputDepName = data.name;
                this.searchValue.departmentId = data.id;
                this.popVisible2 = !this.popVisible2
            },
            onError(err, file, fileList) {
                this.importDataBtnText = '导入数据';
                this.importDataBtnIcon = 'el-icon-upload2';
                this.importDataDisabled = false;
            },
            onSuccess(response, file, fileList) {
                this.importDataBtnText = '导入数据';
                this.importDataBtnIcon = 'el-icon-upload2';
                this.importDataDisabled = false;
                this.initEmps();
            },
            beforeUpload() {
                this.importDataBtnText = '正在导入';
                this.importDataBtnIcon = 'el-icon-loading';
                this.importDataDisabled = true;
            },
            exportData() {
                window.open('/employee/basic/export', '_parent');
            },
            emptyCar() {
                this.accept = {
                    id: "",
                    brand: "",
                    model: "",
                    address: "",
                    telphone: null,
                    carmaster: "",
                    price: 0.00,
                    enginenum: "",
                }
            },
            mallocCar(data) {
                this.title = '分配车辆';
                this.dialogVisible = true;
                this.loading = true;
                this.mallocTo=data.id;
                console.log("malloc",this.mallocTo);
                let url = '/manager/basic/carInfo?page=' + this.page + '&size=' + this.size;
                this.getRequest(url).then(resp => {
                    this.loading = false;
                    if (resp) {
                        this.carMs = resp.data;
                        this.total = resp.total;
                    }
                });
            },
            deleteCar(data) {
                this.$confirm('此操作将永久删除【' + data.id + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/car/basic/" + data.id).then(resp => {
                        if (resp) {
                            this.initCarManager();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAccept() {
                if (this.accept.id) {
                    this.$refs['acceptForm'].validate(valid => {

                        this.putRequest("/manager/basic/" + this.accept.id + "/" + this.accept.accept).then(resp => {
                            if (resp) {
                                this.dialogVisible = false;
                                this.initCarManager();
                            }
                        })

                    });
                } else {
                    this.$refs['acceptForm'].validate(valid => {
                        if (valid) {
                            this.postRequest("/manager/basic/", this.accept).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initCarManager();
                                }
                            })
                        }
                    });
                }
            },
            handleNodeClick(data) {
                this.inputDepName = data.name;
                this.emp.departmentId = data.id;
                this.popVisible = !this.popVisible
            },
            showDepView() {
                this.popVisible = !this.popVisible
            },
            showDepView2() {
                this.popVisible2 = !this.popVisible2
            },
            initData() {
                if (!window.sessionStorage.getItem("nations")) {
                    this.getRequest('/car/basic/nations').then(resp => {
                        if (resp) {
                            this.nations = resp;
                            window.sessionStorage.setItem("nations", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.nations = JSON.parse(window.sessionStorage.getItem("nations"));
                }
                if (!window.sessionStorage.getItem("joblevels")) {
                    this.getRequest('/employee/basic/joblevels').then(resp => {
                        if (resp) {
                            this.joblevels = resp;
                            window.sessionStorage.setItem("joblevels", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.joblevels = JSON.parse(window.sessionStorage.getItem("joblevels"));
                }
                if (!window.sessionStorage.getItem("politicsstatus")) {
                    this.getRequest('/employee/basic/politicsstatus').then(resp => {
                        if (resp) {
                            this.politicsstatus = resp;
                            window.sessionStorage.setItem("politicsstatus", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.politicsstatus = JSON.parse(window.sessionStorage.getItem("politicsstatus"));
                }
                if (!window.sessionStorage.getItem("deps")) {
                    this.getRequest('/employee/basic/deps').then(resp => {
                        if (resp) {
                            this.allDeps = resp;
                            window.sessionStorage.setItem("deps", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.allDeps = JSON.parse(window.sessionStorage.getItem("deps"));
                }
            },
            sizeChange(currentSize) {
                this.size = currentSize;
                this.initEmps();
            },
            currentChange(currentPage) {
                this.page = currentPage;
                this.initEmps();
            },
            showAddCarView() {
                this.emptyCar();
                this.title = '添加车辆';
                this.getMaxWordID();
                this.dialogVisible = true;
            },
            initCarManager(type) {
                this.loading = true;
                let url = '/manager/basic/?page=' + this.page + '&size=' + this.size;
                if (type && type === 'advanced') {
                    if (this.searchValue.name) {
                        url += '&mname=' + this.searchValue.brand;
                    }
                } else {
                    url += "&mname=" + this.keyword;
                }
                this.getRequest(url).then(resp => {
                    this.loading = false;
                    if (resp) {
                        this.CarManagers = resp.data;
                        this.total = resp.total;
                    }
                });
            }
        }
    }
</script>

<style>
    /* 可以设置不同的进入和离开动画 */
    /* 设置持续时间和动画函数 */
    .slide-fade-enter-active {
        transition: all .8s ease;
    }

    .slide-fade-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }

    .slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */
    {
        transform: translateX(10px);
        opacity: 0;
    }
</style>